package com.example.core.controller;

import com.example.core.dao.AS;
import com.example.core.dao.ExportDao;
import com.example.core.dao.NCS;
import com.example.core.dao.TTL;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

/**
 * @program: service
 * @description: ExportController
 * @author: admin
 * @create: 2021-06-25 15:07
 **/
@RestController
public class ExportController {

    @Resource
    ExportDao exportDao;

    @RequestMapping("export")
    public void exportFile(HttpServletResponse response) {

        XSSFWorkbook workbook = new XSSFWorkbook();

        List<TTL> ttlList = exportDao.selectTTL();
        List<NCS> ncsList = exportDao.selectNCS();
        List<AS> asList = exportDao.selectAS();

        Sheet sheet = workbook.createSheet("投资人");
        Row row = sheet.createRow(0);
        row.createCell(0).setCellValue("省份");
        row.createCell(1).setCellValue("大区");
        row.createCell(2).setCellValue("经销商代码");
        row.createCell(3).setCellValue("经销商名称");
        row.createCell(4).setCellValue("年份");
        row.createCell(5).setCellValue("季度");
        row.createCell(6).setCellValue("类型");
        row.createCell(7).setCellValue("税前净利润率");
        row.createCell(8).setCellValue("综合毛利率");
        row.createCell(9).setCellValue("售后吸收率");
        row.createCell(10).setCellValue("费用率");
        row.createCell(11).setCellValue("单店营业收入（万元）");
        row.createCell(12).setCellValue("新车GP%1");
        row.createCell(13).setCellValue("新车GP%2");
        row.createCell(14).setCellValue("新车GP%3");
        row.createCell(15).setCellValue("售后毛利率（不含人工）");
        row.createCell(16).setCellValue("售后产值（万元）");
        row.createCell(17).setCellValue("进场台次");

        for (int i = 0; i < ttlList.size(); i++) {
            Row tempRow = sheet.createRow(i + 1);
            if(ttlList.get(i).get省份() == null){
                tempRow.createCell(0).setCellValue("暂无");
            }else{
                tempRow.createCell(0).setCellValue(ttlList.get(i).get省份());
            }
            if(ttlList.get(i).get大区() == null){
                tempRow.createCell(1).setCellValue("暂无");
            }else{
                tempRow.createCell(1).setCellValue(ttlList.get(i).get大区());
            }


            tempRow.createCell(2).setCellValue(ttlList.get(i).get经销商代码());
            tempRow.createCell(3).setCellValue(ttlList.get(i).get经销商名称());
            tempRow.createCell(4).setCellValue(ttlList.get(i).get年份());
            tempRow.createCell(5).setCellValue(ttlList.get(i).get季度());
            if (ttlList.get(i).get类型().equals("1")) {
                tempRow.createCell(6).setCellValue("Q" + ttlList.get(i).get季度() + "季度");
            } else {
                tempRow.createCell(6).setCellValue("Q" + ttlList.get(i).get季度() + "-YTD");
            }
            tempRow.createCell(7).setCellValue(ttlList.get(i).get税前净利润率());
            tempRow.createCell(8).setCellValue(ttlList.get(i).get综合毛利率());
            tempRow.createCell(9).setCellValue(ttlList.get(i).get售后吸收率());
            tempRow.createCell(10).setCellValue(ttlList.get(i).get费用率());
            tempRow.createCell(11).setCellValue(ttlList.get(i).get单店营业收入());
        }

        for (int i = 0; i < ncsList.size(); i++) {
            Row tempRow = sheet.getRow(i + 1);
            tempRow.createCell(12).setCellValue(ncsList.get(i).get新车GP1());
            tempRow.createCell(13).setCellValue(ncsList.get(i).get新车GP2());
            tempRow.createCell(14).setCellValue(ncsList.get(i).get新车GP3());
        }

        for (int i = 0; i < asList.size(); i++) {
            Row tempRow = sheet.getRow(i + 1);
            if(asList.get(i).get售后毛利率() == null){
                tempRow.createCell(15).setCellValue(0);
            }else{
                tempRow.createCell(15).setCellValue(asList.get(i).get售后毛利率());
            }

            if(asList.get(i).get平均单店售后产值() == null){
                tempRow.createCell(16).setCellValue(0);
            }else{
                tempRow.createCell(16).setCellValue(asList.get(i).get平均单店售后产值());
            }

            if(asList.get(i).get平均单店进场台次() == null){
                tempRow.createCell(17).setCellValue(0);
            }else{
                tempRow.createCell(17).setCellValue(asList.get(i).get平均单店进场台次());
            }




        }


        try (OutputStream outputStream = response.getOutputStream()) {
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/x-download");
            response.setHeader("Content-disposition", "attachment; filename=baseIndex.xlsx");
            workbook.write(outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}
